package com.gmscosmo.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * ClassName: Room
 * Description: 
 * Date: 2021/10/12 15:00 
 * @author stephen.qiu
 * @version 1.0
 *
 */
public class Room {
    private HashMap<String,Participant> participantHashMap;

    public HashMap<String, Participant> getParticipantHashMap() {
        return participantHashMap;
    }

    public void setParticipantHashMap(HashMap<String, Participant> participantHashMap) {
        this.participantHashMap = participantHashMap;
    }

    public List<Participant> getParticipantList() {
        if(participantHashMap==null){
            return new ArrayList<>();
        }
        return participantHashMap.values().stream().collect(Collectors.toList());
    }

    public void addParticipant(Participant participant) {
        if(this.participantHashMap == null){
            participantHashMap = new HashMap(16);
        }
        participantHashMap.put(participant.getDisplay(),participant);
    }

    public void removeParticipant(String userId) {
        if(this.participantHashMap != null) {
            this.participantHashMap.remove(userId);
        }
    }
    public List<Participant> oldGetRemoteParticipant(int nodeId) {
        if(participantHashMap==null){
            return new ArrayList<>();
        }
        Iterator<Participant> iterator = participantHashMap.values().iterator();
        List<Participant> remoteParticipant = new ArrayList<>();
        Participant participant;
        while(iterator.hasNext()){
            participant = iterator.next();
            if(participant.getNode()!= nodeId){
                remoteParticipant.add(participant);
            }
        }
        return remoteParticipant;
    }

    public List<Participant> getRemoteParticipant(int nodeId) {
        if(participantHashMap==null){
            return new ArrayList<>();
        }
        Iterator<Participant> iterator = participantHashMap.values().iterator();
        List<Participant> remoteParticipant = new ArrayList<>();
        Participant participant;
        while(iterator.hasNext()){
            participant = iterator.next();
            if(participant.getNode()!= nodeId && participant.isInRoom()){
                remoteParticipant.add(participant);
            }
        }
        return remoteParticipant;
    }

    public List<Participant> getRemoteParticipant(String userId) {
        if(participantHashMap==null){
            return new ArrayList<>();
        }
        int nodeId = participantHashMap.get(userId).getNode();
        Iterator<Participant> iterator = participantHashMap.values().iterator();
        List<Participant> remoteParticipant = new ArrayList<>();
        Participant participant;
        while(iterator.hasNext()){
            participant = iterator.next();
            if(participant.getNode()!= nodeId && participant.isInRoom()){
                remoteParticipant.add(participant);
            }
        }
        return remoteParticipant;
    }

    @Override
    public String toString() {
        return "Room{" +
                "participantHashMap=" + participantHashMap +
                '}';
    }
}
